package com.xiaoshuidi.cloud.module.bpm.api.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
import com.xiaoshuidi.cloud.module.bpm.api.task.dto.BpmTaskRespDTO;
import com.xiaoshuidi.cloud.module.bpm.dal.dataobject.task.BpmProcessInstanceExtDO;
import com.xiaoshuidi.cloud.module.bpm.enums.task.BpmProcessInstanceResultEnum;
import com.xiaoshuidi.cloud.module.bpm.enums.task.BpmProcessInstanceStatusEnum;
import com.xiaoshuidi.cloud.module.bpm.service.task.BpmProcessInstanceService;
import com.xiaoshuidi.cloud.module.bpm.service.task.BpmTaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Flowable 流程实例 Api 实现类
 *
 * @author
 * @author jason
 */
@RestController
@Validated
public class BpmProcessInstanceApiImpl implements BpmProcessInstanceApi {

    @Resource
    private BpmProcessInstanceService processInstanceService;
    @Resource
    private BpmTaskService bpmTaskService;

    @Override
    public CommonResult<String> createProcessInstance(Long userId, @Valid BpmProcessInstanceCreateReqDTO reqDTO) {
        return CommonResult.success(processInstanceService.createProcessInstance(userId, reqDTO));
    }

    @Override
    public List<BpmTaskRespDTO> getAllTaskListByProcessInstanceId(String processInstanceId) {
        return bpmTaskService.getAllTaskListByProcessInstanceId2(processInstanceId);
    }

    @Override
    public Integer getProcessResult(String processInstanceId) {
        BpmProcessInstanceExtDO bpmProcessInstanceExtDO = processInstanceService.getBpmProcessInstance(processInstanceId);
        if (bpmProcessInstanceExtDO != null && BpmProcessInstanceStatusEnum.FINISH.getStatus().equals(bpmProcessInstanceExtDO.getStatus())) {
            return bpmProcessInstanceExtDO.getResult();
        }
        return BpmProcessInstanceResultEnum.CANCEL.getResult();
    }

    @Override
    public Map<String, Integer> getProcessResultMap(Set<String> processInstanceId) {
        List<BpmProcessInstanceExtDO> l = processInstanceService.getBpmProcessInstances(processInstanceId);
        return l.stream().collect(Collectors.toMap(BpmProcessInstanceExtDO::getProcessInstanceId, p -> {
            if (BpmProcessInstanceStatusEnum.RUNNING.getStatus().equals(p.getStatus())) {
                return BpmProcessInstanceResultEnum.PROCESS.getResult();
            }
            return p.getResult();
        }));
    }

    @Override
    public CommonResult<Map<String, Object>> getProcessVariablesMap(String processInstanceId) {
         BpmProcessInstanceExtDO bpmProcessInstance =
                processInstanceService.getBpmProcessInstance(processInstanceId);
         if(ObjectUtil.isNotEmpty(bpmProcessInstance)){
             return CommonResult.success(bpmProcessInstance.getFormVariables());
         }
         return CommonResult.success(MapUtil.of());
    }

}
